package com.meixiang.adapter.moments;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.moments.ReportList;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/6/20 13:26
 * <p/>
 * 举报适配器
 */
public class AccusationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<ReportList> mList=new ArrayList<>();

    public List<ReportList> getData(){
        return mList;
    }

    private Context context;

    public int selectedPosition= -1;// 选中的位置


    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }


    public void addAll(List<ReportList> mList) {
        this.mList.addAll(mList);
        this.notifyDataSetChanged();
    }
    public void clear(){
        this.mList.clear();
        this.notifyDataSetChanged();
    }


    public AccusationAdapter(Context context) {
        this.context = context;
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item_accusation, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(v,position,((ViewHolder) holder).mIvSelected);
                }
            }
        });
        ((ViewHolder) holder).mIvSelected.setTag(holder);
        Log.e("TAG","得到selectedPosition="+selectedPosition+"position="+position);
        if( selectedPosition == position ){
            ((ViewHolder) holder).mIvSelected.setImageResource(R.mipmap.select_baoxian);
        }else{
            ((ViewHolder) holder).mIvSelected.setVisibility(View.GONE);
        }
        ((ViewHolder) holder).mTvAccusationContent.setText(mList.get(position).getReportComment());



    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
    private onItemClickListener mListener = null;

    public void setOnItemClickListener(onItemClickListener listener) {
        mListener = listener;
    }

    public interface onItemClickListener {
    /**

     * 点赞点击事件

     * @param v

     * @param position

     */
    void onItemClick(View v, int position, ImageView mIvSelected);

    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_accusation_content)
        TextView mTvAccusationContent;
        @Bind(R.id.iv_selected)
        ImageView mIvSelected;
        @Bind(R.id.rl_accusation)
        RelativeLayout mRlAccusation;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
